<template>
  <div class="score-wrap">
    <div class="button-row">
      <el-button type="primary" @click="showNewScoreTaskDialog">新增评分任务</el-button>
    </div>
    <new-score-task-dialog
      :show="showNewTaskDialog"
      @cancle="cancleNewTask"
      @confirmNewTask="confirmNewTask"
    ></new-score-task-dialog>
    <div class="search-row"></div>
    <el-table :data="taskList" stripe style="width: 100%">
      <el-table-column prop="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="name" label="评分任务名称" width="200"></el-table-column>
      <el-table-column prop="statusDesc" label="状态" width="100"></el-table-column>
      <el-table-column prop="totalOrg" label="被评机构数" width="100"></el-table-column>
      <el-table-column prop="finishedOrg" label="已评机构数" width="100"></el-table-column>
      <el-table-column prop="unfinishedOrg" label="未评机构数" width="100"></el-table-column>
      <el-table-column prop="createTime" label="任务创新时间" width="170"></el-table-column>
      <el-table-column prop="updateTime" label="任务最后更新日期" width="170"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            :disabled="scope.row.status != 0"
            @click="configOrg(scope.$index, scope.row)"
          >配置报送机构</el-button>
          <el-button
            size="mini"
            :disabled="scope.row.status != 0"
            @click="markOrg(scope.$index, scope.row)"
            type="success"
          >评分</el-button>
          <el-button size="mini" type="danger" @click="deleteTask(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :total="totalPage"
      @current-change="changePage"
    ></el-pagination>
  </div>
</template>
<script>
import { Button, Input, Table, TableColumn, Pagination } from "element-ui";
import { getUseableModelList } from "@/api/score.js";
import NewScoreTaskDialog from "./components/NewScoreTaskDialog";

import {
  addNewScoreTask,
  getScoreTaskList,
  deleteScoreTask
} from "@/api/score.js";
export default {
  name: "score-manage",
  components: {
    [Button.name]: Button,
    [Input.name]: Input,
    [Table.name]: Table,
    [TableColumn.name]: TableColumn,
    [Pagination.name]: Pagination,
    NewScoreTaskDialog
  },
  data() {
    return {
      showNewTaskDialog: false,
      taskList: [],
      pageNum: 1,
      pageSize: 10,
      totalPage: null,
      keyword: null
    };
  },
  mounted() {
    this.refreshList();
  },
  methods: {
    refreshList() {
      getScoreTaskList({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        keyword: this.keyword
      })
        .then(res => {
          this.taskList = res.list;
          this.totalPage = res.total;
          this.taskList.forEach((item, index) => (item.index = index + 1));
        })
        .catch(errMsg => this.$alert(errMsg.msg));
    },
    configOrg(index, row) {
      this.$router.push({ path: `/score-task-org/${row.id}` });
    },
    markOrg(index, row) {
      this.$router.push({ path: `/score-mark/${row.id}` });
    },
    deleteTask(index, row) {
      this.$confirm("永久删除该评分任务, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteScoreTask(row.id)
            .then(res => {
              this.refreshList();
            })
            .catch(errMsg => this.$alert(errMsg.msg));
        })
        .catch(() => {});
    },
    changePage(pageNum) {
      this.pageNum = pageNum;
      this.refreshList();
    },
    showNewScoreTaskDialog() {
      this.showNewTaskDialog = true;
    },
    cancleNewTask() {
      this.showNewTaskDialog = false;
    },
    confirmNewTask(taskInfo) {
      addNewScoreTask(taskInfo)
        .then(res => {
          this.refreshList();
          this.showNewTaskDialog = false;
        })
        .catch(errMsg => this.$alert(errMsg.msg));
    }
  }
};
</script>
<style lang="stylus" scoped>
.score-wrap {
  padding-left: 5px;
  padding-top: 5px;
}
</style>